<!-- @author zhengjie -->
<template>
  <div class="icon-body">
    <el-input
      v-model="name"
      style="position: relative"
      clearable
      placeholder="请输入图标名称"
      @clear="filterIcons"
      @keyup.enter="filterIcons"></el-input>
    <div class="icon-list">
      <div class="icon-item" v-for="(item, index) in iconList" :key="index" @click="selectedIcon(item)">
        <SvgIcon :name="item" class="svg-icon" size="16"></SvgIcon>
        <el-tooltip :content="item">
          <span class="icon-name">{{ item }}</span>
        </el-tooltip>
      </div>
    </div>
  </div>
</template>

<script setup>
import icons from './requireIcons'
let name = ref('')
let iconList = ref(icons)

const emit = defineEmits(['selected'])

function filterIcons() {
  let list = icons
  if (name.value) {
    iconList.value = list.filter((item) => item.includes(name.value))
  } else {
    iconList.value = list
  }
}
function selectedIcon(name) {
  emit('selected', name)
  document.body.click()
}
function reset() {
  name.value = ''
  iconList.value = icons
}
</script>

<style lang="scss" scoped>
.icon-body {
  width: 100%;
  padding: 10px;
  .icon-list {
    height: 300px;
    display: flex;
    flex-wrap: wrap;
    overflow-y: scroll;
    .icon-item {
      height: 30px;
      cursor: pointer;
      width: 33%;
      display: flex;
      align-items: center;
    }
    .icon-name {
      display: inline-block;
      fill: currentColor;
      overflow: hidden;
      padding: 0 5px;
      white-space: nowrap;
    }
  }
}
</style>
